package com.tjian.fafa.user.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;

@SuppressLint("InflateParams")
public class BaseApplication extends Application
{
    private static String PREF_NAME = "creativelocker.pref";
    private static boolean sIsAtLeastGB;
    static Context _context;

    @Override
    public void onCreate()
    {
        super.onCreate();
        _context = getApplicationContext();
    }

    public static void set(String key, boolean value)
    {
        Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        apply(editor);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static SharedPreferences getPreferences()
    {
        SharedPreferences pre = context().getSharedPreferences(PREF_NAME,
                Context.MODE_MULTI_PROCESS);
        return pre;
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static void apply(Editor editor)
    {
        if (sIsAtLeastGB)
        {
            editor.apply();
        }
        else
        {
            editor.commit();
        }
    }

    public static synchronized BaseApplication context()
    {
        return (BaseApplication) _context;
    }

    public static boolean get(String key, boolean defValue)
    {
        return getPreferences().getBoolean(key, defValue);
    }

}
